/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.swta;

import java.util.HashMap;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

public abstract class ItemPackageEditContentPanel extends Panel {

    String itemId;
    ListView listView;
    MyItemPackage itemPackage;
    ItemPackageForm itemPackageForm;
    WebMarkupContainer container;
    AddItemsToPackageModalWindow addItemWindow;

    public ItemPackageEditContentPanel(String id, Component parent, MyItemPackage itemPackage) {
        super(id);

        this.itemPackage = itemPackage;
        this.container = new WebMarkupContainer("theContainer");

        this.itemPackageForm = new ItemPackageForm("itemPackageForm", parent);
        add(itemPackageForm);
//        this.itemsPanel = new PackageItemsPanel("packageContent");
//        this.itemsPanel.setVisible(true);
//        add(this.itemsPanel);

        this.container.setOutputMarkupId(true);
        this.listView = new ListView("allItems") {

            @Override
            protected void populateItem(ListItem arg0) {
                // nothing yet
            }
        };
        this.container.add(this.listView);
        this.add(container);

        // Create the form, to use later for the buttons
        add(new Label("textLabel", ""));


        this.addItemWindow = new AddItemsToPackageModalWindow("editWindow", this, itemPackage) {

            @Override
            void onCancel(AjaxRequestTarget target) {

                close(target);
            }

            @Override
            void onSelect(AjaxRequestTarget target, String selection) {

                ItemPackageEditContentPanel.this.addListView();
                target.addComponent(ItemPackageEditContentPanel.this.container);
                System.out.println("ONSELECT:ksadjkasjdh");
                close(target);
            }
        };

        add(addItemWindow);

        this.add(new AjaxLink("addItem") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                add(ItemPackageEditContentPanel.this.addItemWindow);

                MyItemPackage sel = MyItemPackageManager.getInstance().getItemPackage(ItemPackageEditContentPanel.this.itemId);
                target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
                ItemPackageEditContentPanel.this.addItemWindow.setTextMessage("Add/ Remove Items to " + sel.getTitle() + " (" + sel.getId() + ").");
                ItemPackageEditContentPanel.this.addItemWindow.setTitleText("Edit of " + sel.getTitle());
//                ItemPackageEditContentPanel.this.selectedID = sel.getId();

                ItemPackageEditContentPanel.this.addItemWindow.setPackageID(ItemPackageEditContentPanel.this.itemId);
//                ((ItemEditContentPanel) ItemActionPanel.this.editWindow.get(ItemActionPanel.this.editWindow.getContentId())).setItemToEdit(sel.getId());
                ItemPackageEditContentPanel.this.addItemWindow.show(target);

            }
        });
    }

    public void populateItem(ListItem item) {

    }

    private void addListView() {

        this.container.remove("allItems");

        this.itemPackage = MyItemPackageManager.getInstance().getItemPackage(ItemPackageEditContentPanel.this.itemId);
        List<MyItem> itemList = itemPackage.getItemList();

        this.listView = new ListView("allItems", itemList) {

            protected void populateItem(ListItem item) {

                MyItem actItem = (MyItem) item.getModelObject();

                item.add(new InDecreasePackageItemPanel("indecrease", ItemPackageEditContentPanel.this.itemId, item.getModel(), ItemPackageEditContentPanel.this.container));
                item.add(new Label("id", actItem.getId()));
                item.add(new Label("category", actItem.getCategory()));
                item.add(new Label("name", actItem.getName()));
                item.add(new Label("weight", "" + actItem.getWeight()));
                item.add(new Label("unit", "" + actItem.getUnit()));
                item.add(new Label("count", "" + ItemPackageEditContentPanel.this.itemPackage.getItems().get(actItem.getId()).intValue()));
                item.add(new Label("price", "" + actItem.getPrice()));
            }
        };

        this.listView.setOutputMarkupId(true);
        this.container.add(this.listView);
//        add(this.listView);
//        add(pContent);

        
    }

    public void setItemToEdit(String itemId) {
        this.itemId = itemId;
        itemPackageForm.setItemPackage(MyItemPackageManager.getInstance().getItemPackage(itemId));
        itemPackageForm.setModelObject(MyItemPackageManager.getInstance().getItemPackage(itemId));

//        this.itemsPanel.setItemToEdit(itemId);
        this.addListView();
    }
    
    public void setMessageText(String text) {
        this.remove("textLabel");
        add(new Label("textLabel", text));
    }

    abstract void onCancel(AjaxRequestTarget target);

    abstract void onSelect(AjaxRequestTarget target, String selection);

}

